package com.thd;

import com.thd.filter.CustomerGatewayFilterFactory;
import com.thd.filter.MyGatewayFilter;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;

import java.util.function.Function;

/**
 * com.thd.GatewayConfig
 *
 * @author: wanglei62
 * @DATE: 2022/4/18 9:36
 **/
@Configuration
public class GatewayConfig {


    @Bean
    public CustomerGatewayFilterFactory customerGatewayFilterFactory(){
        return new CustomerGatewayFilterFactory();
    }


    /**
     * @param routeLocatorBuilder
     * @return org.springframework.cloud.gateway.route.RouteLocator
     * @description 配置了一个id为route_name的路由规则，
     * 当访问地址为http://localhost:9527/sina_redirect时会自动转发到地址：http://www.sina.com.cn
     */
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route(
                "sina_redirect", // 路由id
                r -> r.path("/sina") // 对于路径中含有该内容的url
                        .and() // 并且
                        .method(HttpMethod.GET) // 是get请求
                        .filters( filter -> {
                            filter.filter(new MyGatewayFilter());
                            // 如果多个filter可以这么写
                            // filter.filter(...).filter(...).filter(...);
                            return filter;
                        } ) // 对于满足上面两个条件的请求设置过滤器
                        .uri("http://www.sina.com.cn") // (满足上面两个条件的请求)转发至这个地址
        ).route("csdn_redirect",
                r -> r.path("/csdn")
                        .uri("https://blog.csdn.net/")
        ).build();
        return routes.build();
    }

    @Bean
    public RouteLocator customRouteLocator2(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route(  "cnblogs_redirect",
                new Function<PredicateSpec, Route.AsyncBuilder>() {
                    @Override
                    public Route.AsyncBuilder apply(PredicateSpec predicateSpec) {
                           return predicateSpec.path("/cnblogs").uri("https://www.cnblogs.com/");
                    }
                }
        ).build();
        return routes.build();
    }





    @Bean
    /**
     * 过滤所有路径,无论是否gateway能否找到对应的服务都会进入到这个过滤器中
     */
    public WebFilter printPathFilter() {
        return (ServerWebExchange ctx, WebFilterChain chain) -> {
            ServerHttpRequest request = ctx.getRequest();
            System.out.println("路径：" + request.getURI());
            System.out.println("路径：" + request.getPath());
            return chain.filter(ctx);
        };
    }

}
